package com.nf.health.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.models.DrugStoreBean;
import com.nf.health.app.utils.ImageLoaderUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by changpeng on 2017/5/9 0009.
 */

public class SearchDrugStoreAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<DrugStoreBean.ListBean> mBean;

    public SearchDrugStoreAdapter(Context context, List<DrugStoreBean.ListBean> bean) {
        this.mContext = context;
        this.mBean = bean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_search_drug_store, parent, false);
        return new ContentHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DrugStoreBean.ListBean listBean = mBean.get(position);
        if (listBean == null) {
            return;
        }
        if (holder instanceof ContentHolder) {
            ContentHolder h = (ContentHolder) holder;
            ImageLoaderUtils.getinstance(mContext).getImage(h.mIvDrugIcon, listBean.getHimg(), R.drawable.iv_medicine_img);
            h.mTvName.setText(listBean.getHname());
            h.mTvSpecificAddress.setText(listBean.getAddress());
            h.mLlParent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onClickListener(v,position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (mBean == null) {
            return 0;
        }
        return mBean.size();
    }

    private OnItemClickListener onClickListener;

    public void setOnItemClickListener(OnItemClickListener listener){
        if(onClickListener == null) {
            this.onClickListener = listener;
        }
    }

    public interface OnItemClickListener{
        void onClickListener(View itemView,int position);
    }


    class ContentHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_drug_icon)
        CircleImageView mIvDrugIcon;
        @Bind(R.id.tv_name)
        TextView mTvName;
        @Bind(R.id.tv_specific_address)
        TextView mTvSpecificAddress;
        @Bind(R.id.ll_parent)
        LinearLayout mLlParent;

        public ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
